﻿#include "ResultTreeWidget.h"
#include "GUIEnumType.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossPostData.h"
#include "FITK_Interface/FITKInterfaceStructuralPost/FITKStruPost3DManager.h"
#include "FITK_Interface/FITKInterfaceStructuralPost/FITKStaticStruPostVTK.h"
#include "FITK_Interface/FITKInterfaceStructuralPost/FITKDynamicStruPostVTK.h"

#include "OperatorsInterface/GraphEventOperator.h"
#include "OperatorsInterface/TreeEventOperator.h"
#include <QHeaderView>
#include <QMenu>
#include <QPushButton>
#include <QColorDialog>
#include <QComboBox>

namespace GUI
{
    ResultTreeWidget::ResultTreeWidget(QWidget* parent)
    {
        updateTreeWidget();
    }
    
    Core::FITKTreeWidgetOperator * ResultTreeWidget::getOperator()
    {
        return this->getOperatorT<Core::FITKTreeWidgetOperator>("ResultTreeEvent");
    }
    void ResultTreeWidget::updateTreeWidget()
    {
        // 清空属性树
        this->clear();
        // 隐藏列标题
        this->header()->hide();
        this->blockSignals(true);

        Radioss::FITKRadiossPostData* postData = Radioss::FITKRadiossPostData::GetDataFromAPPFrame();
        if (!postData) return;
        Interface::FITKStruPost3DManager* post3DDataMgr = postData->getPost3DManager();
        if (!post3DDataMgr) return;

        QTreeWidgetItem* postRootItem = this->CreateTreeRootItem(this, QObject::tr("Post"), PostTreeItemType::PITPostRoot);
        if (!postRootItem) return;
        int count = post3DDataMgr->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbstractStructuralPostVTK* post3DData = post3DDataMgr->getDataByIndex(i);
            if (!post3DData) continue;
            QTreeWidgetItem* postItem = this->CreateTreeItem(postRootItem, post3DData->getDataObjectName(), PostTreeItemType::PITPost);
            if (!postItem) continue;
            postItem->setData(0, TreeAdditionalInfo::TAIPostID, post3DData->getDataObjectID());

            QTreeWidgetItem* stepRootItem = this->CreateTreeItem(postItem, QObject::tr("Steps"), PostTreeItemType::PITStepRoot);
            if (!stepRootItem) continue;
            Interface::StruPostType type = post3DData->getPostStructualType();
            if (type == Interface::StruPostType::SPTStatic)
            {
                Interface::FITKStaticStruPostVTK* staticPost3DData = dynamic_cast<Interface::FITKStaticStruPostVTK*>(post3DData);
                if (!staticPost3DData) continue;

            }
            else if (type == Interface::StruPostType::SPTDynamicExplict)
            {
                Interface::FITKDynamicStruPostVTK* dynamicPost3DData = dynamic_cast<Interface::FITKDynamicStruPostVTK*>(post3DData);
                if (!dynamicPost3DData) continue;
                int num = dynamicPost3DData->frameNum();
                for (int i = 0; i < num; ++i)
                {
                    QTreeWidgetItem* stepItem = this->CreateTreeItem(stepRootItem, QString("%1: %2").arg(QObject::tr("Frame")).arg(i), PostTreeItemType::PITStep);
                    stepItem->setData(0, TreeAdditionalInfo::TAIStepIndex, i);
                    stepItem->setData(0, TreeAdditionalInfo::TAIPostID, post3DData->getDataObjectID());
                }
            }
        }

        this->blockSignals(false);
    }
}
